package com.example.android.bluetoothchat.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.android.bluetoothchat.R;
import com.example.android.common.logger.Log;


/**
 * Created by Administrator on 2017/4/21.
 *
 * 蓝牙列表适配器
 */

public class MyBlueBoothAdapter extends BaseListAdapter<BluetoothDevice> {
    
    private static String TAG = "MyBlueBoothAdapter";
    
    public MyBlueBoothAdapter(Context context) {
        super(context);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.blue_tooth_item, null);
        }
        
        TextView tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
        final TextView tvDeviceAddress = (TextView) convertView.findViewById(R.id.tv_device_address);
        BluetoothDevice device = getItem(position);
        
        // 显示蓝牙名称
        tvDeviceName.setText(device.getName());
        
        // 更新蓝牙状态
        switch (device.getBondState()) {
            case BluetoothDevice.BOND_BONDING://正在配对
                Log.d(TAG ,"正在配对......");
                tvDeviceAddress.setVisibility(View.VISIBLE);
                tvDeviceAddress.setText("正在配对...");
                break;
            case BluetoothDevice.BOND_BONDED://配对结束
                Log.d(TAG ,"完成配对");
                tvDeviceAddress.setVisibility(View.GONE);
                tvDeviceAddress.setText("完成配对");
                break;
            case BluetoothDevice.BOND_NONE://取消配对/未配对
                Log.d(TAG ,"取消配对");
                tvDeviceAddress.setVisibility(View.GONE);
                //tvDeviceAddress.setText("取消配对");
                break;
            
        }
        return convertView;
        
    }
}
